package models

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"zy2/doc-serve/config"
)

var DB *gorm.DB

func Init() {
	var err error
	mysqlconfig := config.AppConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlconfig.User,
		mysqlconfig.Password,
		mysqlconfig.Host,
		mysqlconfig.Port,
		mysqlconfig.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("数据库连接成功")
	}
	err = DB.AutoMigrate(&DocServe{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("数据库迁移成功")
	}
}
